package com.bankLogisticsSys.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.bankLogisticsSys.interceptor.ManageLoginInterceptor;
import com.bankLogisticsSys.model.Networkinfo;
import com.bankLogisticsSys.model.Userinfo;
import com.bankLogisticsSys.service.ManageUserService;
import com.bankLogisticsSys.util.Sha1;
import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Record;
@Before(ManageLoginInterceptor.class)
public class ManageUserController extends Controller{
	
	ManageUserService service = new ManageUserService();
	protected int page=1;
	protected int rows=20;
	protected long totalCount;
	
	public void userinfoList(){
		page = getParaToInt("page", 1);
		rows = getParaToInt("rows", 20);
		String sort = getPara("sort","id");
		String order = getPara("order","asc");
		List<Record> list = service.getUserList(getPara("userinfo.name"),page, rows, sort, order);
		totalCount = service.getTotalCount(getPara("userinfo.name"));
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("total", totalCount);
		map.put("rows", list);
		renderJson(map);
	}
	public void networkList(){
		List<Networkinfo> list = service.getNetworkList();
		renderJson(list);
	}

	public void list(){
		render("/WEB-INF/content/user/list.html");
	}
	public void add(){
		render("/WEB-INF/content/user/add.html");
	}
	public void modify(){
		int id = getParaToInt("userinfo.id", 1);
		Userinfo user = service.getUser(id);
		setAttr("userinfo", user);
		render("/WEB-INF/content/user/modify.html");
	}
	
	public void insert(){
		Userinfo user = getModel(Userinfo.class);
		if(!service.isExistEmployeeID(user.getEmployeeID())){
			user.setPassword(Sha1.hex_sha1(user.getPassword()));
			user.setState(0);
			service.insert(user);
			render("/WEB-INF/content/user/list.html");
		}else{
			setAttr("errMsg", "该工号已存在");
			render("/WEB-INF/content/user/add.html");
		}
	}
	
	public void update(){
		Userinfo user = getModel(Userinfo.class);
		service.edite(user);
		render("/WEB-INF/content/user/list.html");
	}
	
	public void delete(){
		Userinfo user = getModel(Userinfo.class);
		Map<String, Integer> map = new HashMap<String, Integer>();
		if(service.delete(user)){
			map.put("result", 0);
		}else{
			map.put("result", 1);
		}
		renderJson(map);
	}
}
